package com.examination.controller.staffModel;

import com.examination.dao.staffModel.DepartmentDao;
import com.examination.entity.BehaviorRecord;
import com.examination.entity.Department;
import com.examination.service.staffModel.DepartmentService;
import com.examination.util.JsonResult;
import com.examination.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author DYF
 */
@Api(description = "部门接口")
@RestController
@RequestMapping("Department")
@CrossOrigin
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "增加部门" , notes = "测试数据:")
    @PostMapping("/InsertDepartment")
    public JsonResult InsertBehavior(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        Department department = departmentService.FindDepartmentByName(map);
        if(department==null){
            int i = departmentService.InsertDepartment(map);
            if (i==1){
                jsonResult.setCode(200);
                jsonResult.setMessage("增加成功!");
                return jsonResult;
            }else {
                jsonResult.setCode(20006);
                jsonResult.setMessage("增加失败!");
                return jsonResult;
            }
        }else {
            jsonResult.setCode(20007);
            jsonResult.setMessage("部门已存在!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "删除部门" , notes = "测试数据:")
    @PostMapping("/DeleteDepartment")
    public JsonResult DeleteDepartment(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = departmentService.DeleteDepartment(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "修改部门" , notes = "测试数据:")
    @PostMapping("/UpdateDepartment")
    public JsonResult UpdateDepartment(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = departmentService.UpdateDepartment(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("修改成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "分页模糊部门信息" , notes = "测试数据:{\"pageNo\":1,\"pageSize\":10,\"user_id\":1}")
    @PostMapping("/FindDepartmentByPage")
    public Page<Department> FindDepartmentByPage(@RequestBody Map map) {
        Page<Department> page = new Page<Department>();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(departmentService.Total(map));
        page.setItems(departmentService.FindDepartmentByPage(map));
        return page;
    }

    @ApiOperation(value = "下拉框查询部门信息" , notes = "测试数据:{\"user_id\":1}")
    @PostMapping("/SelectDepartment")
    public List<Department> SelectDepartment(@RequestBody Map map){
        return departmentService.SelectDepartment(map);
    }
}
